package com.tnmi.user.resolver;

import com.tnmi.context.BaseContextHandler;
import com.tnmi.user.annotation.LoginUser;
import com.tnmi.user.model.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @version 1.0
 * @Classname ContextArgumentResolver
 * @Description TODO
 * @Date 2023-10-21 20:24
 * @Created by tnmi
 **/
@Slf4j
public class ContextArgumentResolver implements HandlerMethodArgumentResolver {


    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.hasParameterAnnotation(LoginUser.class) &&
                methodParameter.getParameterType().equals(SysUser.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter,
                                  ModelAndViewContainer modelAndViewContainer,
                                  NativeWebRequest nativeWebRequest,
                                  WebDataBinderFactory webDataBinderFactory) throws Exception {
        Long userId = BaseContextHandler.getUserId();
        String account = BaseContextHandler.getAccount();
        String name = BaseContextHandler.getName();
        Long orgId = BaseContextHandler.getOrgId();
        Long stationId =BaseContextHandler.getStationId();
        Long tenantId = BaseContextHandler.getTenantId();

        SysUser user = SysUser.builder()
                .id(userId)
                .account(account)
                .name(name)
                .orgId(orgId)
                .stationId(stationId)
                .tenantId(tenantId)
                .build();

        LoginUser loginUser = methodParameter.getMethodAnnotation(LoginUser.class);
        boolean isFull = loginUser.isFull();
        if(isFull|| loginUser.isStation()|| loginUser.isOrg()|| loginUser.isRoles()){
            //用feign 微服务的方式 用用户ID获取 登陆用户中的设置信息

        }
        return user;
    }
}
